var c = require('../../lib/common.js');
const app = getApp()
var cf = require('../../lib/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    sy_address: null,
    sy_addtime: null,
    sy_annimalclass: null,
    sy_annimalsex: null,
    sy_area: null,
    sy_birthday: null,
    sy_city: null,
    sy_lat: null,
    sy_lnt: null,
    sy_mouth: null,
    sy_pic_min: null,
    sy_pr: null,
    sy_qc: null,
    sy_qq: null,
    sy_tel: null,
    username: null,
    sy_vive: null,
    sy_wx: null,
    sy_year: null,
    sy_my: null,
    sy_jy: null,
    head: null,
    sy_describe: null,
    sy_id: null,
    linktype: true,
    buttonclike: false,
    rd_seession: null,
    seestatus: null,
    relation:0,
    topname: '宠物领养小站',
    prepage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var uid = wx.getStorageSync('userinfo').id;
    var that = this;
    var id = options.id;
    var session = options.session;
    var that = this;
    wx.request({
      url: 'https://www.cwlyz.com/wx/details/detailssy?id=' + id + '&session=' + session + '&uid=' + uid, //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log('reggggg');
        console.log(res);
        that.setData({
          sy_id: id
        })
        that.setData({
          sy_describe: res.data.data.sy_describe
        })
        that.setData({
          sy_address: res.data.data.sy_address
        })
        that.setData({
          sy_addtime: res.data.data.sy_addtime
        })
        that.setData({
          sy_annimalclass: res.data.data.sy_annimalclass
        })
        that.setData({
          sy_annimalsex: res.data.data.sy_annimalsex
        })
        that.setData({
          sy_area: res.data.data.sy_area
        })
        that.setData({
          sy_birthday: res.data.data.sy_birthday
        })
        that.setData({
          sy_city: res.data.data.sy_city
        })
        that.setData({
          sy_lat: res.data.data.sy_lat
        })
        that.setData({
          sy_lnt: res.data.data.sy_lnt
        })
        that.setData({
          sy_mouth: res.data.data.sy_mouth
        })
        that.setData({
          sy_pic_min: res.data.data.sy_pic_min
        })
        that.setData({
          sy_pr: res.data.data.sy_pr
        })
        that.setData({
          sy_qc: res.data.data.sy_qc
        })
        that.setData({
          username: res.data.data.username
        })
        that.setData({
          sy_vive: res.data.data.sy_vive
        })
        that.setData({
          sy_year: res.data.data.sy_year
        })
        that.setData({
          sy_my: res.data.data.sy_my
        })
        that.setData({
          sy_jy: res.data.data.sy_jy
        })
        that.setData({
          head: res.data.data.head
        })
        that.setData({
          relation: res.data.data.relation
        })
        console.log('sy_uidsy_uidsy_uidsy_uidsy_uidsy_uid');
        console.log(res.data.data.sy_uid);
        that.setData({
          sy_uid: res.data.data.sy_uid
        })
        if (res.data.data.sy_tel) {
          that.setData({
            sy_tel: res.data.data.sy_tel
          })
          that.setData({
            sy_wx: null
          })
          that.setData({
            sy_qq: null
          })
        } else {
          if (res.data.data.sy_wx) {
            that.setData({
              sy_wx: res.data.data.sy_wx
            })
          } else {
            that.setData({
              sy_qq: res.data.data.sy_qq
            })
          }
        }
        if (res.data.data.seestatus == 1) {
          that.setData({
            seestatus: 1
          })
        } else {
          that.setData({
            seestatus: null
          })
        }
      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.mapCtx = wx.createMapContext('myMap')
    this.mapCtx.getCenterLocation({
      success: function(res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
    this.mapCtx.moveToLocation()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */

  phonebind: function(res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.id //仅为示例，并非真实的电话号码
    })
  },
  clickfuzhi: function(res) {
    var info = res.currentTarget.dataset.info;
    wx.setClipboardData({
      data: info,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  onShareAppMessage: function(res) {
    var that = this;
    var id = this.data.sy_id;
    var sy_describe = '【等待送养】' + this.data.sy_describe;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      //获得session值
      wx.getStorage({
        key: '3rd_seession',
        success: function(res) {
          // console.log(res);
          that.setData({
            rd_seession: res.data.data.rd_session
          });
        }
      })
    }
    return {
      title: sy_describe,
      path: '/pages/details/details?id=' + id,
      success: function(res) {
        var rd_session = that.data.rd_seession;
        that.setData({
          linktype: false
        })
        that.setData({
          buttonclike: true
        })
        wx.request({
          url: 'https://www.cwlyz.com/wx/Index/share_wxadd', //仅为示例，并非真实的接口地址
          data: {
            sid: id,
            uid: rd_session,
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function(res) {
            that.setData({
                showModal: false
              }),
              that.setData({
                seestatus: 1
              }),
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 2000
              })
          }
        })
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },


  onShareTimeline: function (res) {
    var that = this;
    var id = this.data.sy_id;
    var sy_pic_min = this.data.sy_pic_min;
    var sy_describe = '【等待送养】' + this.data.sy_describe;
    console.log(sy_pic_min[0]);
    return {
      title: sy_describe,
      imageUrl: sy_pic_min[0]
    }
  },


  formSubmit: function(e) {
    var uid=wx.getStorageSync('userinfo').id;
    var id = this.data.sy_id;
    var text = e.detail.value.jubao;
    var that = this;
    wx.showLoading({
      title: '数据上传中',
    })
    wx.request({
      url: 'https://www.cwlyz.com/Home/Index/jubaoinfo', //仅为示例，并非真实的接口地址
      data: {
        id: id,
        content: text,
        uid:uid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        wx.hideLoading();
        that.setData({
            showModal: false
          }),
          c.pan('我们会尽快处理!')
      }
    })
  },
  onCancel: function() {
    this.setData({
      showModal: false
    })
  },
  wantjubao: function() {
    this.setData({
      showModal: true
    })
  },
  jibao:function(e){
    var inputvalue=e.detail.value;
    this.setData({ inputvalue: inputvalue})
  },
  gohome:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  apply:function(){
    var pid = this.data.sy_id;
    wx.navigateTo({
      url: '/pages/Apply/apply?pid=' + pid,
    })
  },
  collection:function(){
    wx.showLoading({
      title: '操作中...',
      mask: true,
      success: function(res) {

      },
      fail: function(res) {},
      complete: function(res) {},
    })
    var that=this;
    var relation = that.data.relation==1?0:1;
    var pid = this.data.sy_id;
    var uid = wx.getStorageSync('userinfo').id;
    c.ajax(
      cf.hostapi +'Details/Collection',
      { pid: pid, uid: uid},
      res=>{
        if(res.data.code==100){
          wx.hideLoading();
          c.pan('操作成功！');
          that.setData({ relation: relation})
        };
      }
    )
  },
  talk:function(e){
    console.log('点击消息获取信息');
    console.log(e);
    var did = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: "../chat/chat?did=" + did,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  jumpshequ: function () {
    console.log('jumpshequ');
    wx.navigateToMiniProgram({
      appId: 'wxbfff7aba15880236',
      path: 'pages/index/index',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'trial',
      success(res) {
        console.log(res);
      },
      fail(res) {
        console.log(res);
      }
    })
  }
})